#include "hplcpumppulsecompencurvepage.h"

HplcPumpPulseCompenCurvePage::HplcPumpPulseCompenCurvePage(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("Hplc Pump Pulse Compen Calibrate Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    /*
     * 原始曲线
     */
    m_curve = new Curve();
    m_curve->setTitle("Pulse Compen Curve");
    m_curve->setXAxisName(tr("time"));
    m_curve->addGraph(tr("curve"), QColor(Qt::red));
    m_curve->setScopeFollow(true);

    /*
     * 周期曲线
     */
    m_periodCurve = new Curve();
    m_periodCurve->setTitle("Pulse Compen Curve Period");
    m_periodCurve->setXAxisName(tr("period time"));
    m_periodCurve->addGraph(tr("curve"), QColor(Qt::red));
    m_periodCurve->addGraph(tr("period"), QColor(Qt::green));

    QCustomPlot &plot = m_periodCurve->getQCustomPlot();
    //坐标轴2设置
    plot.xAxis2->setVisible(true);//显示x轴2
    plot.xAxis2->ticker()->setTickCount(32);
    plot.xAxis2->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置
    plot.xAxis2->setRange(0, 32);
    plot.xAxis2->grid()->setVisible(true);
    plot.yAxis2->grid()->setVisible(false);

    //轴内区域设置
    plot.axisRect()->setBackground(QBrush(Qt::black));//背景黑色
    plot.xAxis2->grid()->setPen(QPen(QColor(180, 180, 180), 1, Qt::PenStyle::DashLine));//网格白色虚线
    plot.yAxis->grid()->setPen(QPen(QColor(180, 180, 180), 1, Qt::PenStyle::DashLine));//网格白色虚线
    plot.xAxis2->grid()->setSubGridPen(QPen(QColor(50, 50, 50), 1, Qt::DotLine));//网格浅色点线
    plot.yAxis->grid()->setSubGridPen(QPen(QColor(50, 50, 50), 1, Qt::DotLine));//网格浅色点线
    plot.xAxis2->grid()->setSubGridVisible(true);//显示x轴子网格线
    plot.yAxis->grid()->setSubGridVisible(true);//显示要轴子网格线
    plot.xAxis2->grid()->setZeroLinePen(QPen(Qt::white));//x轴0线颜色白色
    plot.yAxis->grid()->setZeroLinePen(QPen(Qt::white));//y轴0线颜色白色
    plot.xAxis->grid()->setVisible(false);
    plot.yAxis->grid()->setVisible(true);

    m_pbSaveAsPeriod = new QPushButton(tr("Save As Period"));
    connect(m_pbSaveAsPeriod, &QPushButton::clicked, this, &HplcPumpPulseCompenCurvePage::onSaveAsPeriod);

    m_vbl = new QVBoxLayout();
    m_vbl->addWidget(m_curve);
    m_vbl->addWidget(m_periodCurve);
    m_vbl->addWidget(m_pbSaveAsPeriod);
    setLayout(m_vbl);
}

HplcPumpPulseCompenCurvePage::~HplcPumpPulseCompenCurvePage()
{

}

void HplcPumpPulseCompenCurvePage::update(double data)
{
    QCustomPlot &plot = m_curve->getQCustomPlot();

    /* 添加数据 */
    m_curve->onAddGraphData(1, data);
    m_buffer.append(data);

    // 让范围自行缩放，使图0完全适合于可见区域:
    //plot.graph(0)->rescaleAxes();

    plot.replot();
}

void HplcPumpPulseCompenCurvePage::updatePeriod()
{
    QCustomPlot &plot = m_periodCurve->getQCustomPlot();
    //先清除曲线
    plot.graph(0)->data().data()->clear();

    for (int i = 0; i < m_buffer.size(); i++)
    {
//        m_periodCurve->onAddGraphData(1, m_buffer.at(i));
        m_periodCurve->onAddGraphPoint(0, i, m_buffer.at(i));
    }

    // 让范围自行缩放，使图0完全适合于可见区域:
    //plot.graph(0)->rescaleAxes();

    plot.replot();

    m_buffer.clear();//清理周期曲线数据
}

void HplcPumpPulseCompenCurvePage::closeEvent(QCloseEvent *event)
{
    QWidget::closeEvent(event);
}

void HplcPumpPulseCompenCurvePage::onSaveAsPeriod()
{
    QCustomPlot &plot = m_periodCurve->getQCustomPlot();
    //先清除曲线
    plot.graph(1)->data().data()->clear();

    // 复制曲线0的数据给曲线1
    for (auto it = plot.graph(0)->data().data()->begin(); it != plot.graph(0)->data().data()->end(); it++)
    {
        plot.graph(1)->addData(it->key, it->value);
    }

    // 让范围自行缩放，使图1完全适合于可见区域:
    //plot.graph(1)->rescaleAxes();

    plot.replot();
}
